import WebHeader from '@/www/components/WebHeader'
import WebFooter from '@/www/components/WebFooter'
import { getQuote, uploadFile, postQuoteOrder } from '@/apiv2/quote'

const CODE = 'PvcMemberCard'
const COUNT_PER_UNIT = 100

export default {
  components: { WebHeader, WebFooter },
  data() {
    return {
      positionCount: undefined,
      quoteData: undefined,

      checkValue: {

      },

      quoteValue: {
        category: undefined,
        style: 1,
        side: '单面',
        quantity: undefined,
        size: undefined
      },

      processSelectOptions: {
        side: [
          {value: 'single', label: '单面'},
          {value: 'double', label: '双面'},
        ]
      },

      user: undefined,
      customSize: false,
      quote: undefined,
      quoteList: undefined,
      detail: undefined,
      fileList: [],
      logic: undefined,
      postData: {
        quantity: undefined,
        detail: undefined,
        amount: undefined,
        fileIds: undefined
      }
    }
  },

  created(){
    const user = JSON.parse(localStorage.getItem('user'))
    this.user = user

    getQuote(CODE).then(res => {
      let quoteData = res.config
      this.quoteValue.category = quoteData.cateData.list[0]
      this.quoteValue.size = quoteData.sizeData.list[0]
      this.quoteValue.quantity = quoteData.stepData.list[0]
      this.quoteData = quoteData
    })
  },

  methods: {

    getPricePerPosition(){
      let {quoteData,quoteValue} = this
      let price = undefined
      quoteData.cateData.list.forEach(item => {
        if(item.name == quoteValue.category){
          price = item.price
        }
      })
      return price
    },

    getStepMaterialDiscount(qty){
      let {quoteData,quoteValue} = this
      let discount = undefined
      quoteData.stepDiscountData.list.forEach(item => {
        if(qty >= parseInt(item.start) && qty <= parseInt(item.end)){
          discount = parseFloat(item.discount)
        }
      })
      return discount ? discount : parseFloat(quoteData.stepDiscountData.list[quoteData.stepDiscountData.list.length-1].discount)
    },

    getPriceFromStampingSize(size){
      let quoteData = this.quoteData
      let price = 0.0;
      quoteData.process.stamping.forEach(item => {
        if(size == item.name){
          price = parseFloat(item.price)
        }
      })
      return price
    },

    getQuote1(){
      let detail = ''
      let {quoteData,quoteValue,checkValue,customSize,postData} = this
      const {cardPosition} = quoteData.param
      let length = undefined
      let width = undefined
      if(quoteValue.customLength && quoteValue.customWidth && customSize){
        length = parseInt(quoteValue.customLength)
        width = parseInt(quoteValue.customWidth)
      }else if(quoteValue.size){
        length = parseInt(quoteValue.size.split('*')[0])
        width = parseInt(quoteValue.size.split('*')[1])
      }

      detail += '尺寸' + length + '*' + width +'，'
        + quoteValue.category +'，'
        + quoteValue.style + '款，'
        + quoteValue.side + '，'
        + (parseInt(quoteValue.quantity) * parseInt(quoteValue.style)) + '盒，'
      console.log(detail)

      const POSITION_LENGTH = parseInt(cardPosition.split('*')[0])
      const POSITION_WIDTH = parseInt(cardPosition.split('*')[1])
      const POSITION_COUNT = parseInt(this.caculatePositionCount(length,width,POSITION_LENGTH,POSITION_WIDTH))

      if(quoteValue.quantity > 5 && quoteValue < 10){
        quoteValue.quantity = 10
        this.quoteValue = quoteValue
      }

      let quoteList = []

      quoteData.stepData.list.forEach(i => {
        const QTY = parseInt(i) * parseInt(quoteValue.style)//阶梯盒数
        const QUANTITY = QTY * COUNT_PER_UNIT//阶梯张数
        const PROCESS_QUANTITY = parseFloat(QUANTITY / 500)

        let sku = undefined
        quoteData.skuList.forEach(item => {
          if(item.name == quoteValue.category && parseInt(item.quantity) == parseInt(i)){
            sku = item
          }
        })

        const MATERIAL_AMOUNT = parseFloat(sku.price) * parseFloat(quoteValue.style) * parseFloat(POSITION_COUNT)

        let processAmount = 0

        const AMOUNT = parseInt(MATERIAL_AMOUNT) + parseInt(processAmount)


        if(parseInt(quoteValue.quantity) == parseInt(i)){
          postData.detail = detail
          postData.amount = AMOUNT
        }

        quoteList.push({
          quantity: QTY,
          materialAmount: parseInt(MATERIAL_AMOUNT),
          processAmount: parseInt(processAmount),
          totalAmount: AMOUNT

        })

        this.quoteList = quoteList
        this.positionCount = POSITION_COUNT
      })

    },

    caculatePositionCount(customLength,customWidth,length,width){
      let count1 = (length > customLength ? 1 : Math.ceil(customLength / length)) *
                   (width > customWidth ? 1 : Math.ceil(customWidth / width))
      let count2 = (width > customLength ? 1 : Math.ceil(customLength / width)) *
                   (length > customWidth ? 1 : Math.ceil(customWidth / length))
      return count1 >= count2 ? count2 : count1
    },

    submitOrder(){
      const {fileList,postData} = this

      if(fileList.length < 1){
        this.$message.error('请上传设计稿')
        return
      }

      let fileIds = [];
      fileList.forEach(item => {
        fileIds.push(item.id)
      })
      postData.fileIds = fileIds

      const loading = this.$loading({
        lock: true,
        text: '正在创建订单',
        spinner: 'el-icon-loading',
        background: 'rgba(0,0,0,0.7)'
      });

      postQuoteOrder(postData).then(res => {
        loading.close()
        this.$message.success('成功创建订单')
        this.$router.push({
          path: '/submitOrder',
          query: { id : res.id}
        })
      }).catch(err => {
        loading.close()
        this.$message.error('创建订单失败')
      })
    },

    handleUpload: async function(data){
      const formData = new FormData()
      formData.append('file', data.file)
      const res = await uploadFile(formData)
      let fileList = this.fileList
      fileList.push(res.file)
      this.fileList = fileList
      console.log(fileList)
    },

    getStepPriceFromList(value,list){
      let stepPrice = undefined
      list.forEach(i => {
        if(value == i.text){
          stepPrice = i.stepPrice
        }
      })
      return stepPrice
    },
  }
}
